/*
 * Copyright (C) 2008 Shane Isbell
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.slideme.atom.parser;

import org.kxml2.io.KXmlParser;
import org.slideme.atom.parser.entity.AtomEntityContext;
import org.xmlpull.v1.XmlPullParserException;

import java.io.IOException;

public class Link {
    private String href;

    private String rel, type, hreflang, title, length;

    public String getHref() {
        return href;
    }

    public void setHref(String href) {
        this.href = href;
    }

    public String getRel() {
        return rel;
    }

    public void setRel(String rel) {
        this.rel = rel;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getHreflang() {
        return hreflang;
    }

    public void setHreflang(String hreflang) {
        this.hreflang = hreflang;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getLength() {
        return length;
    }

    public void setLength(String length) {
        this.length = length;
    }

    public static Link marshalFrom(KXmlParser parser, AtomEntityContext ctx) throws IOException, XmlPullParserException {
        if (parser == null) {
            throw new IllegalArgumentException("parser");
        }
        Link link = new Link();
        int attributeCount = parser.getAttributeCount();
        if (attributeCount == 0) {
            throw new IOException("Missing required href attribute");
        }
        for (int i = 0; i < attributeCount; i++) {
            String name = parser.getAttributeName(i);
            if (name.equals("href")) {
                link.setHref(parser.getAttributeValue(i));
            } else if (name.equals("rel")) {
                link.setRel(parser.getAttributeValue(i));
            } else if (name.equals("type")) {
                link.setType(parser.getAttributeValue(i));
            } else if (name.equals("hreflang")) {
                link.setHreflang(parser.getAttributeValue(i));
            } else if (name.equals("title")) {
                link.setTitle(parser.getAttributeValue(i));
            } else if (name.equals("length")) {
                link.setLength(parser.getAttributeValue(i));
            }
        }
        if(link.getHref() == null) {
            throw new IOException("Missing required href attribute");
        }
        parser.nextTag();
        return link;
    }
}
